package com.powernode.driver.service.impl;


import com.powernode.driver.client.CiFeignClient;
import com.powernode.driver.service.FileService;
import com.powernode.driver.service.MonitorService;
import com.powernode.model.entity.order.OrderMonitor;
import com.powernode.model.entity.order.OrderMonitorRecord;
import com.powernode.model.form.order.OrderMonitorForm;
import com.powernode.model.vo.order.TextAuditingVo;
import com.powernode.order.client.OrderMonitorFeignClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class MonitorServiceImpl implements MonitorService {

    @Resource
    private FileService fileService;

    @Resource
    private OrderMonitorFeignClient orderMonitorFeignClient;

    @Resource
    private CiFeignClient ciFeignClient;


    /**
     * 上传音频到minio中
     * 上传文字到mongodb
     * @param file
     * @param orderMonitorForm
     * @return
     */
    @Override
    public Boolean upload(MultipartFile file, OrderMonitorForm orderMonitorForm) {
        //上传音频到minio中
        String url = fileService.upload(file);

        OrderMonitorRecord orderMonitorRecord = new OrderMonitorRecord();
        orderMonitorRecord.setOrderId(orderMonitorForm.getOrderId());
        orderMonitorRecord.setFileUrl( url);//音频文件的路径
        orderMonitorRecord.setContent(orderMonitorForm.getContent());

        //审核文字
        TextAuditingVo textAuditingVo = ciFeignClient.textAuditing(orderMonitorRecord.getContent()).getData();
        orderMonitorRecord.setResult(textAuditingVo.getResult());
        orderMonitorRecord.setKeywords(textAuditingVo.getKeywords());


        orderMonitorFeignClient.saveMonitorRecord(orderMonitorRecord);


        OrderMonitor orderMonitor = orderMonitorFeignClient.getOrderMonitor(orderMonitorRecord.getOrderId()).getData();
        int fileNum = orderMonitor.getFileNum() + 1;
        orderMonitor.setFileNum(fileNum);

        //需要审核的文件个数  2表示疑似敏感
        if ("2".equals(orderMonitorRecord.getResult())) {
            int audiNum = orderMonitor.getAuditNum() + 1;

            orderMonitor.setAuditNum(audiNum);
        }

        orderMonitorFeignClient.updateOrderMonitor(orderMonitor);

        return true;
    }
}
